<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/1 0001
 * Time: 上午 9:50
 */
header("Content-type:text/html;charset=utf-8");
header("Access-Control-Allow-Origin:*");

$connection = new mysqli("127.0.0.1","root","","mytps138");

mysqli_query($connection,"set names utf8");

//前台传输的数据;
//var num = $("#number").val();
//var detail = $("#detail");
//var goid = detail.data("goid");
//var goprice = detail.data("goprice");
//var goname = detail.data("goname");
//var uid = $.cookie("userid");


if (!empty($_POST) && $_POST["uid"] != null && $_POST["uid"] != ""){

    $uid = $_POST["uid"];//用户ID
    $num = $_POST["num"];//商品数量
    $goid = $_POST["goid"];//商品ID
    $goprice = $_POST["goprice"];//商品价格
    $goname = $_POST["goname"];//商品名字
    $goprice1 = $_POST["goprice1"];//商品数值价格；
    $goSimg = $_POST["goSimg"];


    $sql = 'SELECT*FROM cart WHERE uid = "'.$uid.'" AND goid = "'.$goid.'"';

    $res = $connection ->query($sql);

//    print_r($res);

    if ($res->num_rows == 1){
        //数据库有记录;
        $sqlUpdate = "UPDATE mytps138.cart SET carnumber = carnumber + ".$num." WHERE uid = ".$uid." AND goid = ".$goid;
//        print_r($sqlUpdate);//test
        $resUpdate = $connection->query($sqlUpdate);
        if($resUpdate){
            $arrUpdate = array("status"=>1);
            print_r(json_encode($arrUpdate));
        }else {
            $arrUpdate = array("status"=>0);
            print_r(json_encode($arrUpdate));
        }

    }else{
        //没有记录就添加;
        $sqlAdd = "INSERT INTO `mytps138`.`cart` (carnumber,goid,gostatus,goname,goprice,uid,goSimg,goprice1) VALUES('". $num ."','".$goid."',1,'".$goname."','".$goprice."','".$uid."','$goSimg','$goprice1')";
        $resAdd = $connection->query($sqlAdd);
//        print_r($resAdd);
        if($resAdd){
            $arrAdd = array("status"=>3);
            print_r(json_encode($arrAdd));
        }else {
            $arrAdd = array("status"=>4);
            print_r(json_encode($arrAdd));
        }
    }


}
$connection ->close();
















